import path from "path";
import fs from "fs";
import robot  from 'robotjs';
const outputPath = require('path').join(process.cwd(), `mouse_operations_${Date.now()}.json`);
export function formatDate(date:Date){
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    return `${year}${month}${day}${hour}`;
}
export async function delay(n: number) {
  return new Promise(r => {
    setTimeout(() => {
      r(null);
    }, n);
  })
}

// 鼠标操作数据的接口定义
export interface MouseOperation {
  timestamps: number[];
  positions: Array<{ xPercent: number; yPercent: number }>;
}
export async function recordMouseOperations(){
  console.log("开始录制鼠标操作，将记录10个操作，每个操作5秒");
  
  // 获取屏幕尺寸
  const screenSize = robot.getScreenSize();
  const screenWidth = screenSize.width;
  const screenHeight = screenSize.height;
  
  const operations: Array<{
      timestamps: number[];
      positions: Array<{ xPercent: number; yPercent: number }>;
  }> = [];
  
  for (let i = 0; i < 10; i++) {
      console.log(`\n准备录制第 ${i + 1}/10 个操作，5秒后开始...`);
      await delay(2000);
      console.log(`3...`);
      await delay(1000);
      console.log(`2...`);
      await delay(1000);
      console.log(`1...`);
      await delay(1000);
      console.log(`开始录制！请在5秒内完成操作...`);
      
      const startTime = Date.now();
      const endTime = startTime + 5000; // 5秒
      
      const timestamps: number[] = [];
      const positions: Array<{ xPercent: number; yPercent: number }> = [];
      
      // 设置采样间隔（毫秒）
      const sampleInterval = 50; // 每50毫秒采样一次，即每秒20次
      
      // 创建采样定时器
      const samplingInterval = setInterval(() => {
          // 获取当前鼠标位置
          const mousePos = robot.getMousePos();
          
          // 计算百分比位置
          const xPercent = parseFloat((mousePos.x / screenWidth * 100).toFixed(2));
          const yPercent = parseFloat((mousePos.y / screenHeight * 100).toFixed(2));
          
          // 记录时间戳（相对于开始时间的毫秒数）
          const timestamp = Date.now() - startTime;
          
          timestamps.push(timestamp);
          positions.push({ xPercent, yPercent });
          
          console.log(`采样点 [${timestamp}ms]: 位置 ${xPercent}%, ${yPercent}%`);
      }, sampleInterval);
      
      // 等待5秒
      await new Promise(resolve => setTimeout(resolve, 5000));
      
      // 停止采样
      clearInterval(samplingInterval);
      
      // 保存这次操作的数据
      operations.push({
          timestamps,
          positions
      });
      
      console.log(`第 ${i + 1} 个操作录制完成！共记录了 ${positions.length} 个采样点`);
      
      // 最后一个操作后不需要等待
      if (i < 9) {
          console.log("请稍等，准备录制下一个操作...");
          await delay(2000);
      }
  }
  
  // 将数据保存到JSON文件
  const outputPath = path.join(process.cwd(), `mouse_operations_${Date.now()}.json`);
  fs.writeFileSync(outputPath, JSON.stringify(operations, null, 2));
  return operations;
  console.log(`\n录制完成！共录制了 ${operations.length} 个操作`);
  console.log(`数据已保存到: ${outputPath}`);
}